﻿/*
 * Copyright(C) 2010,ajayumi 保留所有权利。( All rights reserved. )
 * 
 * 文件名称：SqlAjMembershipHelper.cs
 * 摘    要：
 * 当前版本：1.0
 * 作    者：黄乙冬 (ajayumi)
 * 创建日期：2010年10月29日星期二
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ajayumi.Platform.Web.Security
{
    internal class SqlAjMembershipHelper
    {
        internal static Guid GetApplicationId(string connectionString, string applicationName)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT ApplicationId FROM [aspnet_Applications] " +
                          "WHERE ApplicationName = @ApplicationName", conn))
                {
                    cmd.Parameters.Add("@ApplicationName", SqlDbType.NVarChar, 256).Value = applicationName;

                    conn.Open();
                    var applicationId = cmd.ExecuteScalar();
                    if (applicationId == null)
                    {
                        throw new System.Configuration.Provider.ProviderException("Unable to find application id for provided application name: " + applicationName);
                    }
                    return (Guid)(applicationId);

                }
            }
        }

        private static IList<string> GetTableNames(SqlConnection testConn)
        {
            var dt = testConn.GetSchema("Tables");
            IList<string> tables = new List<string>();
            foreach (DataRow r in dt.Rows)
            {
                tables.Add(r["TABLE_NAME"].ToString());
            }
            return tables;
        }
    }
}
